package com.iut.projectandroid;

import com.iut.utils.MenuHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ConvertisseurActivity extends Activity {

	private float poids = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_convertisseur);
		init();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.alternative, menu);
		return true;
	}

	public void init() 
	{
		final EditText editDensite = (EditText) findViewById(R.id.densiteSemi);
		final EditText editPMG = (EditText) findViewById(R.id.pmg);
		final TextView resPoid = (TextView) findViewById(R.id.poidsSemenceNec);
		final Button btnCmd = (Button)findViewById(R.id.button_commande);
		
		editDensite.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				calcul(editDensite, editPMG, resPoid);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		editPMG.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				calcul(editDensite, editPMG, resPoid);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	
		btnCmd.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				Intent i = new Intent(getApplicationContext(), CommandeActivity.class);
				i.putExtra("POID_SEMENCE", String.format("%.2f", poids));
				i.putExtra("DENSITE_SEMIS", editDensite.getText().toString());
				startActivity(i);
			}
		});
	}

	private void calcul(EditText editDensite, EditText editPMG, TextView resPoid) 
	{
		/* Afficher le bouton que si il y a des informations remplies */
		if((editDensite.getText() != null && editDensite.getText().toString().equals("")) || 
				(editPMG.getText() != null && editPMG.getText().toString().equals(""))){
			final Button btnCmd = (Button)findViewById(R.id.button_commande);
			btnCmd.setEnabled(true);
		}
		try 
		{
			float d = Float.parseFloat(editDensite.getText().toString()
					.replace(",", "."));
			float pmg = Float.parseFloat(editPMG.getText().toString()
					.replace(",", "."));
			poids = d * pmg;
			resPoid.setText(String.format("%.2f", poids) + " Kg/ha");
		} catch (NumberFormatException e) {
			resPoid.setText("");
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		if(MenuHelper.launchActivity(this ,getApplicationContext(), item.getItemId()))
		{
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
	}
}
